package com.itboye.lottery.activity.user

import android.content.Intent
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.TextView
import com.itboye.bylibrary.listener.MyClickListener
import com.itboye.lottery.BuildConfig
import com.itboye.lottery.R
import com.itboye.lottery.adapter.ChongZhiJineAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.OrderBean
import com.itboye.lottery.callback.PayCallBack
import com.itboye.lottery.dialog.PayDialog
import com.itboye.lottery.presenter.UserPresenter
import com.itboye.lottery.retrofit.HttpResponse
import com.itboye.lottery.retrofit.ResponseCallback
import kotlinx.android.synthetic.main.activity_chongzhi.*
import java.util.*

class ChongzhiActivity : BaseActivity(), ResponseCallback {
    override fun success(o: Any, requestSuccessType: String) {
        if (requestSuccessType == userPresenter!!.chongzhi_SUCCESS) {
            var orderBean = o as HttpResponse<OrderBean>
            val payDialog = PayDialog(this, true)
            payDialog.setChongzhi(true)
            payDialog.setOrderBean(orderBean.data)
//            payDialog.setAmount(orderBean.data.money.toDouble())
            payDialog.setCallBack(object : PayCallBack {
                override fun paySuccess() {
                    finish()
                }

                override fun payFail(msg: String) {

                }
            })
            payDialog.show()
        }
        if (requestSuccessType == userPresenter!!.chongzhiByFs_SUCCESS) {
            ByAlert.alert(o)
        }
    }

    override fun fail(code: Int, msg: Any, requestErrorType: String) {
        if (requestErrorType == userPresenter!!.chongzhi_FAIL) {
            ByAlert.alert(msg)
        }
        if (requestErrorType == userPresenter!!.chongzhiByFs_FAIL) {
            ByAlert.alert(msg)
        }
    }

    var rv_chongzhi: RecyclerView? = null
    var adapter: ChongZhiJineAdapter? = null
    var userPresenter: UserPresenter = UserPresenter(this)
    var tv_chongzhi_sure: TextView? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_chongzhi
    }

    private lateinit var array: ArrayList<HashMap<String, Any>>

    override fun initData() {
        rv_chongzhi!!.layoutManager = GridLayoutManager(this, 4)
        array = getData()
        adapter = ChongZhiJineAdapter(array)
        adapter!!.setOnItemClickListener { _, position ->
            if (array[position]["status"].toString().toBoolean()) {
                return@setOnItemClickListener
            }
            for (i in array.indices) {
                array[i]["status"] = position == i
            }
            adapter!!.notifyDataSetChanged()
        }
        rv_chongzhi!!.adapter = adapter
        rightTextValue("充值明细", MyClickListener {
            startActivity(Intent(this, ChongZhi_TiXianMingXi_ZhangHuMingXi_ListActivity::class.java).putExtra("type", 3).putExtra("page", 1))
        })
        cb_chongzhi.isChecked = true
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_chongzhi_sure -> {
//                if (!cb_chongzhi.isChecked) {
//                    ByAlert.alert("请选择充值方式")
//                    return
//                }
                var money = adapter!!.selectMoney
                val payDialog = PayDialog(this, true)
                payDialog.setChongzhi(true)
//                payDialog.setOrderBean(orderBean.data)
//                payDialog.setAmount(money)
                if (BuildConfig.IS_DEBUG) {
//                    payDialog.setAmount(0.01)
                    payDialog.setAmount(money)
                } else {
                    payDialog.setAmount(money)
                }
                payDialog.setCallBack(object : PayCallBack {
                    override fun paySuccess() {
                        finish()
                    }

                    override fun payFail(msg: String) {

                    }
                })
                payDialog.show()
//                userPresenter!!.chongzhiByFs(money)
            }
            else -> {
            }
        }
    }

    var arrayListMoney = arrayOf("10", "50", "100", "500", "1000", "5000", "10000", "20000")
    fun getData(): ArrayList<HashMap<String, Any>> {
        var arrayList = ArrayList<HashMap<String, Any>>()
        for (i in 0..arrayListMoney.size - 1) {
            var hashMap = HashMap<String, Any>()
            hashMap["text"] = "${arrayListMoney[i]}元"
            hashMap["value"] = arrayListMoney[i]
            hashMap["status"] = i == 0
            arrayList.add(hashMap)
        }
        return arrayList
    }
}
